package com.ssps.back.property.service.impl;

import com.ssps.back.property.service.PropertyService;
import com.ssps.entity.*;
import com.ssps.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PropertyServiceImpl implements PropertyService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    ParkingMapper parkingMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    ComplaintMapper complaintMapper;
    @Autowired
    AmountAccountMapper amountAccountMapper;


    @Override
    public Map<String, Object> selectCountStatus(Integer year) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 1; i <=12 ; i++) {
            map.put("finish"+i,0);
            map.put("unfinished"+i,0);
        }
        System.out.println(map);

        String status = "已完成";
        List<Count> list = ordersMapper.selectStatusByYear(year, status);
        System.out.println(list.size());

        for (Count count : list) {
            System.out.println(count);
            map.put("finish"+count.getMonth(),count);
        }
        String status1 = "未出租";
        List<Count> list2 = ordersMapper.selectStatusByYear(year, status1);
        for (Count count : list2) {
            map.put("unfinished"+count.getMonth(),count.getCount());
        }

        return map;
    }

    @Override
    public List<Parking> selectAll(String info, User loginUser, String statuses) {
        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        List<Parking> list3 = new ArrayList<>();
//        UserInfoExample example = new UserInfoExample();
//        UserInfoExample.Criteria criteria1 = example.createCriteria();
//        criteria1.andUidEqualTo(loginUser.getUid());
//        List<UserInfo> userInfos1 = userInfoMapper.selectByExample(example);
//        UserInfo userInfo2 = userInfos1.get(0);
        List<User> users = userMapper.selectByExample(null);
        for (User user : users) {
            list.add(user.getUid());

        }
        UserInfoExample example2 = new UserInfoExample();
        UserInfoExample.Criteria criteria = example2.createCriteria();
        criteria.andUidIn(list);
        if (info != null && !info.trim().equals("")) {
            criteria.andUnameLike("%" + info + "%");
            List<UserInfo> userInfos = userInfoMapper.selectByExample(example2);
            if (userInfos.size() == 0) {
                List<User> users2 = userMapper.selectByExample(null);
                for (User user : users2) {
                    list.add(user.getUid());

                }
                UserInfoExample example5 = new UserInfoExample();
                UserInfoExample.Criteria criteria2 = example2.createCriteria();
                criteria2.andUidIn(list);
                List<UserInfo> userInfos2 = userInfoMapper.selectByExample(example5);
                for (UserInfo userinfo : userInfos2) {
                    // if( userinfo.getCid()== userInfo2.getCid()){
                    list2.add(userinfo.getUiid());
                    //  }
                }
                ParkingExample example3 = new ParkingExample();
                ParkingExample.Criteria criteria3 = example3.createCriteria();
                if (statuses.equals("已出租")) {
                    criteria3.andUiidIn(list2);
                    criteria3.andPnumberLike("%" + info + "%");
                    criteria3.andStatusEqualTo("已出租");
                    List<Parking> parkingList = parkingMapper.selectByExample(example3);
                    if (parkingList.size() == 0) {
                        return list3;
                    }
                    return parkingList;
                } else if (statuses.equals("未出租")) {
                    criteria3.andUiidIn(list2);
                    criteria3.andPnumberLike("%" + info + "%");
                    criteria3.andStatusEqualTo("未出租");
                    List<Parking> parkingList = parkingMapper.selectByExample(example3);
                    if (parkingList.size() == 0) {
                        return list3;
                    }
                    return parkingList;
                }
                criteria3.andUiidIn(list2);
                criteria3.andPnumberLike("%" + info + "%");
                List<Parking> parkingList = parkingMapper.selectByExample(example3);
                if (parkingList.size() == 0) {
                    return list3;
                }
                return parkingList;

            } else {
                for (UserInfo userinfo : userInfos) {
                    // if( userinfo.getCid()== userInfo2.getCid()){
                    list2.add(userinfo.getUiid());
                    //  }
                }
                if (statuses.equals("已出租")) {
                    ParkingExample example3 = new ParkingExample();
                    ParkingExample.Criteria criteria2 = example3.createCriteria();
                    criteria2.andUiidIn(list2);
                    criteria2.andStatusEqualTo("已出租");
                    List<Parking> parkingList = parkingMapper.selectByExample(example3);
                    return parkingList;
                } else if (statuses.equals("未出租")) {
                    ParkingExample example3 = new ParkingExample();
                    ParkingExample.Criteria criteria2 = example3.createCriteria();
                    criteria2.andUiidIn(list2);
                    criteria2.andStatusEqualTo("未出租");
                    List<Parking> parkingList = parkingMapper.selectByExample(example3);
                    return parkingList;
                }
                ParkingExample example3 = new ParkingExample();
                ParkingExample.Criteria criteria2 = example3.createCriteria();
                criteria2.andUiidIn(list2);
                List<Parking> parkingList = parkingMapper.selectByExample(example3);
                return parkingList;
            }
        } else {
            List<UserInfo> userInfos = userInfoMapper.selectByExample(example2);
            for (UserInfo userinfo : userInfos) {
                // if( userinfo.getCid()== userInfo2.getCid()){
                list2.add(userinfo.getUiid());
                //  }
            }
            if (statuses.equals("已出租")) {
                ParkingExample example3 = new ParkingExample();
                ParkingExample.Criteria criteria2 = example3.createCriteria();
                criteria2.andUiidIn(list2);
                criteria2.andStatusEqualTo("已出租");
                List<Parking> parkingList = parkingMapper.selectByExample(example3);
                return parkingList;
            } else if (statuses.equals("未出租")) {
                ParkingExample example3 = new ParkingExample();
                ParkingExample.Criteria criteria2 = example3.createCriteria();
                criteria2.andUiidIn(list2);
                criteria2.andStatusEqualTo("未出租");
                List<Parking> parkingList = parkingMapper.selectByExample(example3);
                return parkingList;
            }
            ParkingExample example3 = new ParkingExample();
            ParkingExample.Criteria criteria2 = example3.createCriteria();
            criteria2.andUiidIn(list2);
            List<Parking> parkingList = parkingMapper.selectByExample(example3);
            return parkingList;
        }
    }

    @Override
    public List<GoodPercent> selecStatementPercent(Integer year) {
        List<GoodPercent> list = commentMapper.selectStatementPercent(year);
        for (GoodPercent goodPercent : list) {
            goodPercent.setGood(goodPercent.getFive() + goodPercent.getFour());
            goodPercent.setNormal(goodPercent.getThree() + goodPercent.getTwo());
            goodPercent.setBad(goodPercent.getOne());
        }
        return list;
    }

    @Override
    public Map<String, Integer> selectAllOrders() {
        Map<String, Integer> map = new HashMap<>();
        List<Orders> orders = ordersMapper.selectByExample(null);
        map.put("orders", orders.size());
        return map;
    }

    @Override
    public Map<String, Integer> selectAllParking() {
        Map<String, Integer> map = new HashMap<>();
        List<Parking> parkingList = parkingMapper.selectByExample(null);
        map.put("parking", parkingList.size());
        return map;
    }

    @Override
    public Map<String, Object> selectAllMoney() {
        Map<String, Object> map = new HashMap<>();
        double returnmoney = 0.0;
        List<AmountAccount> amountAccounts = amountAccountMapper.selectByExample(null);
        map.put("plantformmoney", amountAccounts.get(0).getPlatformAmount());
        map.put("frozenmoney", amountAccounts.get(0).getAmountFrozen());
        map.put("propertymoney", amountAccounts.get(0).getPropertyAmount());
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo("已退款");
        List<Orders> orders = ordersMapper.selectByExample(example);
        for (Orders order : orders) {
            returnmoney = +order.getPrice();
        }
        map.put("returnmoney", returnmoney);

        return map;
    }

    @Override
    public Map<String, Double> selectMoney() {
        Map<String, Double> map = new HashMap<>();
        List<AmountAccount> amountAccounts = amountAccountMapper.selectByExample(null);
        BigDecimal amount = amountAccounts.get(0).getPropertyAmount();
        double v = amount.doubleValue();
        map.put("money", v);
        return map;
    }

    @Override
    public Map<String, Integer> selectAllComplain() {
        Map<String, Integer> map = new HashMap<>();
        List<Complaint> complaints = complaintMapper.selectByExample(null);
        map.put("complain", complaints.size());
        return map;
    }
}





